<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\activity;


use app\common\model\activity\Activity;
use app\common\logic\BaseLogic;
use app\common\model\template\Template;
use app\common\service\FileService;
use think\facade\Db;


/**
 * RechargeActivity逻辑
 * Class ActivityLogic
 * @package app\adminapi\logic
 */
class ActivityLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $background = Template::where('id', $params['template_id'])->value('background') ?? '';
            Activity::create([
                'admin_id'    => $params['admin_id'],
                'app_id'      => $params['app_id'],
                'template_id' => $params['template_id'] ?? 0,
                'image'       => $params['image'] ? FileService::setFileUrl($params['image']) : '',
                'title'       => $params['title'],
                'content'     => $params['content'],
                'background'  => $background,
                'describe'    => $params['describe'],
                'remark'      => $params['remark'] ?? '',
                'button'      => $params['button'],
                'money'       => $params['money'],
                'k_coin'      => $params['k_coin'],
                'path'        => $params['path'] ?? '',
                'limit_num'   => $params['limit_num'],
                'position'    => $params['position'],
                'scope'       => $params['scope'],
                'start_time'  => strtotime($params['start_time']),
                'end_time'    => strtotime($params['end_time']),
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            $background = Template::where('id', $params['template_id'])->value('background') ?? '';
            Activity::where('id', $params['id'])->update([
                'template_id' => $params['template_id'] ?? 0,
                'image'       => $params['image'] ? FileService::setFileUrl($params['image']) : '',
                'title'       => $params['title'],
                'content'     => $params['content'],
                'background'  => $background,
                'describe'    => $params['describe'],
                'remark'      => $params['remark'] ?? '',
                'button'      => $params['button'],
                'money'       => $params['money'],
                'k_coin'      => $params['k_coin'],
                'path'        => $params['path'] ?? '',
                'limit_num'   => $params['limit_num'],
                'position'    => $params['position'],
                'scope'       => $params['scope'],
                'start_time'  => strtotime($params['start_time']),
                'end_time'    => strtotime($params['end_time']),
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function delete(array $params): bool
    {
        return Activity::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function detail($params): array
    {
        return Activity::findOrEmpty($params['id'])->toArray();
    }


    /**
     * @notes  更改状态
     * @param array $params
     * @return bool
     * @author heshihu
     * @date 2022/2/21 18:04
     */
    public static function updateStatus(array $params)
    {

        Db::startTrans();
        try {
            Activity::update([
                'id'     => $params['id'],
                'status' => $params['status']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
}
